package com.dream.common.utils;

import com.dream.entity.UserInfo;
import org.springframework.web.socket.WebSocketSession;

import javax.servlet.http.HttpSession;


public class SessionUtils {
    //当前用户的信息
    public static final String USER_INFO = "USER_INFO";
    //用于识别webSocket
    public static final String SESSION_ID = "SESSION_ID";

    /**
     * 获取用户信息(基于http)
     */
    public static UserInfo getUserInfo(HttpSession session){
        Object userInfo = session.getAttribute(USER_INFO);
        if (userInfo == null) {
            return null;
        }
        return (UserInfo) userInfo;
    }

    /**
     * 获取用户信息(基于websocket)
     */
    public static UserInfo getUserInfoFromWebSocket(WebSocketSession session){
        Object userInfo = session.getAttributes().get(USER_INFO);
        return (UserInfo) userInfo;
    }



    public static String getSessionIdFromWebSocketSession(WebSocketSession session) {
        Object sessionId = session.getAttributes().get(SESSION_ID);
        if(sessionId == null){
            return null;
        }
        return (String) sessionId;
    }
}
